{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy \n",
    "import gensim\n",
    "import numpy as np\n",
    "from jieba import analyse\n",
    "from gensim.models import Word2Vec\n",
    "from gensim.models.word2vec import LineSentence"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_vector(file_name,model):\n",
    "    with open(file_name,'r')as f:\n",
    "        wordvec_size=200\n",
    "        word_vector=numpy.zeros(wordvec_size)\n",
    "        for data in f:\n",
    "            space_pos=get_pos(data,' ')\n",
    "            first_word=data[0:space_pos[0]]\n",
    "            if model.wv.__contains__(first_word):\n",
    "                word_vector=word_vector+model.wv[first_word]\n",
    "            for i in range(len(space_pos)-1)：\n",
    "                word=data[space_pos[i]:space_pos[i+1]]\n",
    "                if model.wv.__contains__(word):\n",
    "                    word_vector=word_vector+model.wv[word]\n",
    "        return word_vector"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def similarity(vector1,vector2):\n",
    "    vector1_abs=np.sqrt(vector1.dot(vector1))\n",
    "    vector2_abs=np.sqrt(vector2.dot(vector2))\n",
    "    if vector2_abs !=0 and vector1_abs !=0:\n",
    "        similarity = (vector1.dot(vector2))/(vector1_abs * vector2_abs)\n",
    "    else:\n",
    "        similarity = 0\n",
    "    return similarity"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def main():\n",
    "    model=gensim.models.Word2Vec.load('model_word2vec')\n",
    "    new1='new1.txt'\n",
    "    new2='new2.txt'\n",
    "    new1_keywords='new1_keywords.txt'\n",
    "    new2_keywords='new2_keywords.txt'\n",
    "    get_keywords(new1,new1_keywords)\n",
    "    get_keywords(new2,new2_keywords)\n",
    "    new1_vector=get_vector(new1_keywords,model)\n",
    "    print('文本new1的部分向量:\\n',new1_vector[:20])\n",
    "    new2_vector=get_vector(new2_keywords,model)\n",
    "    print('文本new2的部分向量:\\n',new2_vector[:20])\n",
    "    print('文本new1和文本new2的相似度：'，similarity(new1_vector,new2_vector ))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if __name__ == \"__main__\"\n",
    "main()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
